package com.example.springbootlucenedemo.common.lucene.impl;

import com.example.springbootlucenedemo.common.lucene.IndexService;
import com.example.springbootlucenedemo.common.lucene.annotation.LuceneIndexPath;
import com.example.springbootlucenedemo.common.lucene.bean.Message;
import com.example.springbootlucenedemo.common.lucene.conf.LuceneConf;
import com.example.springbootlucenedemo.common.lucene.conf.RedisConf;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
@ConditionalOnProperty(name="spring.lucene.pub/subModel",havingValue="true",matchIfMissing = false)
public class MessageSend<V> implements IndexService<V> {
    @Autowired
    RedisTemplate redisTemplate;
    LuceneConf luceneConf;
    IndexServiceImpl indexService;
    @Autowired
    public MessageSend(LuceneConf luceneConf){
        this.luceneConf =luceneConf;
        this.indexService = new IndexServiceImpl(this.luceneConf);
    }
    @Override
    public void addDocument(Iterable<V> iterable) {
        iterable.forEach(it->{
            redisTemplate.convertAndSend(RedisConf.RedisMessageTopic.addDocument,new Message(it));
        });
    }

    @Override
    public Long addDocument(V object) {
        redisTemplate.convertAndSend(RedisConf.RedisMessageTopic.addDocument,new Message(object));
        return 0L;
    }

    @Override
    public Page search(com.example.springbootlucenedemo.common.lucene.bean.Page pageRequest) {
        return indexService.search(pageRequest);
    }

    @Override
    public List<Map<String, Object>> search(int pageNumber, int size, Sort sort, Query query) {
        return indexService.search(pageNumber,size,sort,query);
    }

    @Override
    public Long deleteDocument(V obj) {
        redisTemplate.convertAndSend(RedisConf.RedisMessageTopic.deleteDocument,new Message(obj));
         return 0L;
    }

    @Override
    public Long updateDocument(V obj) {
        redisTemplate.convertAndSend(RedisConf.RedisMessageTopic.updateDocument,new Message(obj));
        return 0L;
    }



}
